package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.bulkstock.service.TemporaryCreditLineService;
import com.yuexunit.finance.member.entity.TemporaryCreditLineEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
*@version 创建时间：2016年9月9日 上午10:47:42
*@author wenwenlong
*类说明
*/

@Implements(TemporaryCreditLineService.class)
public class TemporaryCreditLineServiceImpl implements TemporaryCreditLineService {

	@Override
	@Transactional
	public Long createTemporaryCreditLine(TemporaryCreditLineEntity entity) {
		entity.setEffective(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getEnterpriseId();
	}

	@Override
	@Transactional
	public void updateTemporaryCreditLine(TemporaryCreditLineEntity entity) {
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void disableTemporaryCreditLine(Long enterpriseId) {
		TemporaryCreditLineEntity entity = PersistUtil.get(TemporaryCreditLineEntity.class, enterpriseId);
		entity.setEffective(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	public TemporaryCreditLineEntity inquireTemporaryCreditLine(Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditLineEntity.class);
		criteria.add(Restrictions.eq("effective", Boolean.TRUE));
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		List<TemporaryCreditLineEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(list.size() - 1);
	}
	
	@Override
	public TemporaryCreditLineEntity inquireTemporaryCreditLine1(Long enterpriseId) {
		return PersistUtil.get(TemporaryCreditLineEntity.class, enterpriseId);
	}

	@Override
	public PageList<TemporaryCreditLineEntity> inquireTemporaryCreditLinePageList(PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditLineEntity.class);
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<TemporaryCreditLineEntity> inquireTemporaryCreditLineList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(TemporaryCreditLineEntity.class);
		return PersistUtil.get(criteria);
	}

}
